package com.inei.evaluacionmype.controller.activity;

import com.inei.evaluacionmype.R;
import com.inei.evaluacionmype.controller.CapituloIVController;
import com.inei.evaluacionmype.model.entity.Cap04;
import com.inei.evaluacionmype.tools.Mensaje;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TableRow;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.RadioButton;

public class CapituloIV_Activity extends Fragment {

	// componentes
	private View root;
	private TableRow trOpcional;
	private EditText etRuc, etInf, etTel, etMov, etRed, etInte, etC4_21,
			etC4_22, etC4_3, etC4_5, etC4_7, etC4_9, etC4_11_O, etC4_a, etC4_g;
	private RadioButton rbInf1, rbInf2, rbTel1, rbTel2, rbMov1, rbMov2, rbRed1,
			rbRed2, rbInte1, rbInte2, rbIntr1, rbIntr2, rbExtr1, rbExtr2,
			rbC4_51, rbC4_52, rbC4_61, rbC4_62, rbC4_81, rbC4_82, rbC4_101,
			rbC4_102, rbC4_131, rbC4_132, rbC4_141, rbC4_142, rbC4_151,
			rbC4_152, rbC4_161, rbC4_162, rbC4_171, rbC4_172;
	private CheckBox cbC4_11_11, cbC4_11_12, cbC4_11_13, cbC4_11_14,
			cbC4_11_21, cbC4_11_22, cbC4_11_23, cbC4_11_O, cbC4_a1, cbC4_a2,
			cbC4_a3, cbC4_a4, cbC4_b1, cbC4_c1, cbC4_d1, cbC4_e1, cbC4_f1,
			cbC4_g1;

	// variables
	private CapituloIVController con;
	private Cap04 obj;
	private String ruc_id;
	private boolean nuevo = false;
	private Mensaje mensaje;

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		root = inflater
				.inflate(R.layout.activity_capitulo_iv, container, false);

		return root;
	}

	@Override
	public void onViewCreated(View view, Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onViewCreated(view, savedInstanceState);

		con = new CapituloIVController(getActivity());
		mensaje = new Mensaje(getActivity());

		IniciarComponentes();

		CargarDatos();

		AgregarEventos();
	}

	private void IniciarComponentes() {
		trOpcional = (TableRow) root.findViewById(R.id.trOpcional);

		etRuc = (EditText) root.findViewById(R.id.etRuc);
		etInf = (EditText) root.findViewById(R.id.etInf);
		etTel = (EditText) root.findViewById(R.id.etTel);
		etMov = (EditText) root.findViewById(R.id.etMov);
		etRed = (EditText) root.findViewById(R.id.etRed);
		etInte = (EditText) root.findViewById(R.id.etInte);
		etC4_21 = (EditText) root.findViewById(R.id.etC4_21);
		etC4_22 = (EditText) root.findViewById(R.id.etC4_22);
		etC4_3 = (EditText) root.findViewById(R.id.etC4_3);
		etC4_5 = (EditText) root.findViewById(R.id.etC4_5);
		etC4_7 = (EditText) root.findViewById(R.id.etC4_7);
		etC4_9 = (EditText) root.findViewById(R.id.etC4_9);
		etC4_11_O = (EditText) root.findViewById(R.id.etC4_11_O);
		etC4_a = (EditText) root.findViewById(R.id.etC4_a);
		etC4_g = (EditText) root.findViewById(R.id.etC4_g);

		rbInf1 = (RadioButton) root.findViewById(R.id.rbInf1);
		rbInf2 = (RadioButton) root.findViewById(R.id.rbInf2);
		rbTel1 = (RadioButton) root.findViewById(R.id.rbTel1);
		rbTel2 = (RadioButton) root.findViewById(R.id.rbTel2);
		rbMov1 = (RadioButton) root.findViewById(R.id.rbMov1);
		rbMov2 = (RadioButton) root.findViewById(R.id.rbMov2);
		rbRed1 = (RadioButton) root.findViewById(R.id.rbRed1);
		rbRed2 = (RadioButton) root.findViewById(R.id.rbRed2);
		rbInte1 = (RadioButton) root.findViewById(R.id.rbInte1);
		rbInte2 = (RadioButton) root.findViewById(R.id.rbInte2);
		rbIntr1 = (RadioButton) root.findViewById(R.id.rbIntr1);
		rbIntr2 = (RadioButton) root.findViewById(R.id.rbIntr2);
		rbExtr1 = (RadioButton) root.findViewById(R.id.rbExtr1);
		rbExtr2 = (RadioButton) root.findViewById(R.id.rbExtr2);
		rbC4_51 = (RadioButton) root.findViewById(R.id.rbC4_51);
		rbC4_52 = (RadioButton) root.findViewById(R.id.rbC4_52);
		rbC4_61 = (RadioButton) root.findViewById(R.id.rbC4_61);
		rbC4_62 = (RadioButton) root.findViewById(R.id.rbC4_62);
		rbC4_81 = (RadioButton) root.findViewById(R.id.rbC4_81);
		rbC4_82 = (RadioButton) root.findViewById(R.id.rbC4_82);
		rbC4_101 = (RadioButton) root.findViewById(R.id.rbC4_101);
		rbC4_102 = (RadioButton) root.findViewById(R.id.rbC4_102);
		rbC4_131 = (RadioButton) root.findViewById(R.id.rbC4_131);
		rbC4_132 = (RadioButton) root.findViewById(R.id.rbC4_132);
		rbC4_141 = (RadioButton) root.findViewById(R.id.rbC4_141);
		rbC4_142 = (RadioButton) root.findViewById(R.id.rbC4_142);
		rbC4_151 = (RadioButton) root.findViewById(R.id.rbC4_151);
		rbC4_152 = (RadioButton) root.findViewById(R.id.rbC4_152);
		rbC4_161 = (RadioButton) root.findViewById(R.id.rbC4_161);
		rbC4_162 = (RadioButton) root.findViewById(R.id.rbC4_162);
		rbC4_171 = (RadioButton) root.findViewById(R.id.rbC4_171);
		rbC4_172 = (RadioButton) root.findViewById(R.id.rbC4_172);

		cbC4_11_11 = (CheckBox) root.findViewById(R.id.cbC4_11_11);
		cbC4_11_12 = (CheckBox) root.findViewById(R.id.cbC4_11_12);
		cbC4_11_13 = (CheckBox) root.findViewById(R.id.cbC4_11_13);
		cbC4_11_14 = (CheckBox) root.findViewById(R.id.cbC4_11_14);
		cbC4_11_21 = (CheckBox) root.findViewById(R.id.cbC4_11_21);
		cbC4_11_22 = (CheckBox) root.findViewById(R.id.cbC4_11_22);
		cbC4_11_23 = (CheckBox) root.findViewById(R.id.cbC4_11_23);
		cbC4_11_O = (CheckBox) root.findViewById(R.id.cbC4_11_O);
		cbC4_a1 = (CheckBox) root.findViewById(R.id.cbC4_a1);
		cbC4_a2 = (CheckBox) root.findViewById(R.id.cbC4_a2);
		cbC4_a3 = (CheckBox) root.findViewById(R.id.cbC4_a3);
		cbC4_a4 = (CheckBox) root.findViewById(R.id.cbC4_a4);
		cbC4_b1 = (CheckBox) root.findViewById(R.id.cbC4_b1);
		cbC4_c1 = (CheckBox) root.findViewById(R.id.cbC4_c1);
		cbC4_d1 = (CheckBox) root.findViewById(R.id.cbC4_d1);
		cbC4_e1 = (CheckBox) root.findViewById(R.id.cbC4_e1);
		cbC4_f1 = (CheckBox) root.findViewById(R.id.cbC4_f1);
		cbC4_g1 = (CheckBox) root.findViewById(R.id.cbC4_g1);

		etInf.setEnabled(false);
		etTel.setEnabled(false);
		etMov.setEnabled(false);
		etRed.setEnabled(false);
		etInte.setEnabled(false);
		etC4_7.setEnabled(false);
		etC4_9.setEnabled(false);
		etC4_11_O.setEnabled(false);
		etC4_a.setEnabled(false);
		etC4_g.setEnabled(false);

		trOpcional.setVisibility(View.GONE);
	}

	private void AgregarEventos() {
		EventoSiNo(rbInf1);
		EventoSiNo(rbInf2);
		EventoSiNo(rbTel1);
		EventoSiNo(rbTel2);
		EventoSiNo(rbMov1);
		EventoSiNo(rbMov2);
		EventoSiNo(rbRed1);
		EventoSiNo(rbRed2);
		EventoSiNo(rbInte1);
		EventoSiNo(rbInte2);
		EventoSiNo(rbC4_61);
		EventoSiNo(rbC4_62);
		EventoSiNo(rbC4_81);
		EventoSiNo(rbC4_82);

		EventoOtro(cbC4_11_O);
		EventoOtro(cbC4_a4);
		EventoOtro(cbC4_g1);
	}

	private void EventoSiNo(final RadioButton rb) {
		rb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (rb == rbInf1 || rb == rbInf2) {
					etInf.requestFocus();
					etInf.setEnabled(rbInf1.isChecked() ? true : false);
					etInf.setText(rbInf2.isChecked() ? "" : null);
				}
				if (rb == rbTel1 || rb == rbTel2) {
					etTel.requestFocus();
					etTel.setEnabled(rbTel1.isChecked() ? true : false);
					etTel.setText(rbTel2.isChecked() ? "" : null);
				}
				if (rb == rbMov1 || rb == rbMov2) {
					etMov.requestFocus();
					etMov.setEnabled(rbMov1.isChecked() ? true : false);
					etMov.setText(rbMov2.isChecked() ? "" : null);
				}
				if (rb == rbRed1 || rb == rbRed2) {
					etRed.requestFocus();
					etRed.setEnabled(rbRed1.isChecked() ? true : false);
					etRed.setText(rbRed2.isChecked() ? "" : null);
				}
				if (rb == rbInte1 || rb == rbInte2) {
					etInte.requestFocus();
					etInte.setEnabled(rbInte1.isChecked() ? true : false);
					etInte.setText(rbInte2.isChecked() ? "" : null);
					trOpcional.setVisibility(rbInte1.isChecked() ? View.VISIBLE
							: View.GONE);
				}
				if (rb == rbC4_61 || rb == rbC4_62) {
					etC4_7.requestFocus();
					etC4_7.setEnabled(rbC4_61.isChecked() ? true : false);
					etC4_7.setText(rbC4_62.isChecked() ? "" : null);
				}
				if (rb == rbC4_81 || rb == rbC4_82) {
					etC4_9.requestFocus();
					etC4_9.setEnabled(rbC4_81.isChecked() ? true : false);
					etC4_9.setText(rbC4_82.isChecked() ? "" : null);
				}
			}
		});
	}

	private void EventoOtro(final CheckBox cb) {
		cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {

			@Override
			public void onCheckedChanged(CompoundButton buttonView,
					boolean isChecked) {
				if (cb == cbC4_11_O) {
					etC4_11_O.requestFocus();
					etC4_11_O.setEnabled(cbC4_11_O.isChecked() ? true : false);
					etC4_11_O.setText(cbC4_11_O.isChecked() ? null : "");
				}
				if (cb == cbC4_a4) {
					etC4_a.requestFocus();
					etC4_a.setEnabled(cbC4_a4.isChecked() ? true : false);
					etC4_a.setText(cbC4_a4.isChecked() ? null : "");
				}
				if (cb == cbC4_g1) {
					etC4_g.requestFocus();
					etC4_g.setEnabled(cbC4_g1.isChecked() ? true : false);
					etC4_g.setText(cbC4_g1.isChecked() ? null : "");
				}
			}
		});
	}

	private void CargarDatos() {
		ruc_id = getArguments().getString("ruc_id");
		obj = new Cap04();
		obj.setRUC(ruc_id);

		obj = con.Cap04Obtener(obj);
		etRuc.setText(ruc_id);
		if (obj != null) {

		}
	}

	public void ValidarCap() {

	}

	public void GuardarCap() {
		if (obj == null) {
			nuevo = true;
			obj = new Cap04();
			obj.setRUC(ruc_id);
		}

		obj.setC4AP1_INFRA_COMPU(revisarSiNo(rbInf1, rbInf2));
		obj.setC4AP1_INFRA_COMPU_CANT(Integer.parseInt(etInf.getText()
				.toString().trim().length() == 0 ? "0" : etInf.getText()
				.toString()));
		obj.setC4AP1_INFRA_TELF_FIJA(revisarSiNo(rbTel1, rbTel2));
		obj.setC4AP1_INFRA_TELF_FIJA_CANT(Integer.parseInt(etTel.getText()
				.toString().trim().length() == 0 ? "0" : etTel.getText()
				.toString()));
		obj.setC4AP1_INFRA_TELF_MOVIL(revisarSiNo(rbMov1, rbMov2));
		obj.setC4AP1_INFRA_TELF_MOVIL_CANT(Integer.parseInt(etMov.getText()
				.toString().trim().length() == 0 ? "0" : etMov.getText()
				.toString()));
		obj.setC4AP1_SERVI_RED(revisarSiNo(rbRed1, rbRed2));
		obj.setC4AP1_SERVI_RED_CANT(Integer.parseInt(etRed.getText().toString()
				.trim().length() == 0 ? "0" : etRed.getText().toString()));
		obj.setC4AP1_SERVI_INTERNET(revisarSiNo(rbInte1, rbInte2));
		obj.setC4AP1_SERVI_INTERNET_CANT(Integer.parseInt(etInte.getText()
				.toString().trim().length() == 0 ? "0" : etInte.getText()
				.toString()));
		obj.setC4AP1_SERVI_INTRANET(revisarSiNo(rbIntr1, rbIntr2));
		obj.setC4AP1_SERVI_EXTRANET(revisarSiNo(rbExtr1, rbExtr2));
		obj.setC4AP2_CANT(Integer.parseInt(etC4_21.getText().toString().trim()
				.length() == 0 ? "0" : etC4_21.getText().toString()));
		obj.setC4AP2_TOTAL(Integer.parseInt(etC4_22.getText().toString().trim()
				.length() == 0 ? "0" : etC4_22.getText().toString()));
		obj.setC4AP3_PORCENTAJE(Double.parseDouble(etC4_3.getText().toString()
				.trim().length() == 0 ? "0" : etC4_3.getText().toString()));
		obj.setC4BP4_PORCENTAJE_ACCESO(Double.parseDouble(etC4_5.getText()
				.toString().trim().length() == 0 ? "0" : etC4_5.getText()
				.toString()));
		obj.setC4BP5_SITIOWEB(revisarSiNo(rbC4_51, rbC4_52));
		obj.setC4CP6_COMPRA(revisarSiNo(rbC4_61, rbC4_62));
		obj.setC4CP7_COMPRA_PORCENTAJE(Double.parseDouble(etC4_7.getText()
				.toString().trim().length() == 0 ? "0" : etC4_7.getText()
				.toString()));
		obj.setC4CP8_VENDE(revisarSiNo(rbC4_81, rbC4_82));
		obj.setC4CP9_VENDE_PORCENTAJE(Double.parseDouble(etC4_9.getText()
				.toString().trim().length() == 0 ? "0" : etC4_9.getText()
				.toString()));
		obj.setC4DP10_CONEXIONES(revisarSiNo(rbC4_101, rbC4_102));
		obj.setC4DP11_BANCHA_MODEM(cbC4_11_11.isChecked() ? 1 : 0);
		obj.setC4DP11_BANCHA_OTROS(cbC4_11_12.isChecked() ? 1 : 0);
		obj.setC4DP11_BANCHA_SATELITAL(cbC4_11_13.isChecked() ? 1 : 0);
		obj.setC4DP11_BANCHA_INTER_MOVIL(cbC4_11_14.isChecked() ? 1 : 0);
		obj.setC4DP11_BANGOSTA_MODEM(cbC4_11_21.isChecked() ? 1 : 0);
		obj.setC4DP11_BANGOSTA_RDSI(cbC4_11_22.isChecked() ? 1 : 0);
		obj.setC4DP11_BANGOSTA_INTER_MOVIL(cbC4_11_23.isChecked() ? 1 : 0);
		obj.setC4DP11_ESPECIF_OTRO(cbC4_11_O.isChecked() ? 1 : 0);
		obj.setC4DP11_ESPECIFIQUE(etC4_11_O.getText().toString());
		obj.setC4EP12A_BUSQ_PRODUC(cbC4_a1.isChecked() ? 1 : 0);
		obj.setC4EP12A_BUSQ_ORGANIS(cbC4_a2.isChecked() ? 1 : 0);
		obj.setC4EP12A_BUSQ_ACTIVI(cbC4_a3.isChecked() ? 1 : 0);
		obj.setC4EP12A_BUSQ_OTROS(cbC4_a4.isChecked() ? 1 : 0);
		obj.setC4EP12A_BUSQ_ESPECIF(etC4_a.getText().toString());
		obj.setC4EP12B_COMUNICACIONES(cbC4_b1.isChecked() ? 1 : 0);
		obj.setC4EP12C_OPERACIONES(cbC4_c1.isChecked() ? 1 : 0);
		obj.setC4EP12D_TRANSACCIONES(cbC4_d1.isChecked() ? 1 : 0);
		obj.setC4EP12E_SERVICIO(cbC4_e1.isChecked() ? 1 : 0);
		obj.setC4EP12F_DISTRIBUIR(cbC4_f1.isChecked() ? 1 : 0);
		obj.setC4EP12G_OTRAS(cbC4_g1.isChecked() ? 1 : 0);
		obj.setC4EP12G_ESPECIFIQUE(etC4_g.getText().toString());
		obj.setC4FP13_SW_PROPIETARIO(revisarSiNo(rbC4_131, rbC4_132));
		obj.setC4FP14_SW_LIBRE(revisarSiNo(rbC4_141, rbC4_142));
		obj.setC4FP15_SW_PROPIO(revisarSiNo(rbC4_151, rbC4_152));
		obj.setC4FP16_EMP_NACIONAL(revisarSiNo(rbC4_161, rbC4_162));
		obj.setC4GP17_CAPACITACION(revisarSiNo(rbC4_171, rbC4_172));

		if (nuevo) {
			if (con.Cap04Insertar(obj) > 0) {
				mensaje.Exito("SE GRABO CON EXITO");
				nuevo = false;
			} else {
				mensaje.Error("ERROR AL GRABAR");
			}
		} else {
			if (con.Cap04Modificar(obj) > 0) {
				mensaje.Exito("SE MODIFICO CON EXITO");
			} else {
				mensaje.Error("ERROR AL MODIFICAR");
			}
		}
	}

	private int revisarSiNo(RadioButton rb1, RadioButton rb2) {
		return (rb1.isChecked() ? 1 : (rb2.isChecked() ? 2 : 0));
	}
}
